﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using Tea;
using Tea.Utils;
using Newtonsoft.Json;
using GrapeCity.Forguncy.Commands;
using static dingtalk_Card.dingtalk_CardServerCommand;
using System.Runtime.CompilerServices;
using System.Windows.Documents;
using System.Collections;
using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics.Metrics;

namespace dingtalk_Card
{
    internal class dingcard
    {
        public static ExecuteResult CreateErrorJson(int errorCode, string errorMessage) //定义一个错误返回的静态方法
        {
            return new ExecuteResult() { ErrCode = errorCode, Message = errorMessage };
        }

        public static AlibabaCloud.SDK.Dingtalkcard_1_0.Client CreateClient()
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
            config.Protocol = "https";
            config.RegionId = "central";
            return new AlibabaCloud.SDK.Dingtalkcard_1_0.Client(config);
        }

        


        //卡片数据内容方法初始化
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCardData> CreateCardData(IServerCommandExecuteContext dataContext, IEnumerable<kuozhan> aaa)
        {
            // 创建一个字典来存储卡片数据内容
            var cardDataCardParamMap = new Dictionary<string, string>();

            // 先检查 数据是否为空
            if (aaa == null || !aaa.Any())
            {
                 CreateErrorJson(101, "卡片数据内容不能为空");
                return null; // 停止执行
            }

            // 遍历卡片模板内容替换参数列表
            foreach (var param in aaa)
            {
                string value = null;
                //对参数内容值做公式计算
                var key = (await dataContext.EvaluateFormulaAsync(param.参数名))?.ToString() ?? string.Empty;
                //先计算后判断
                var value1 = await dataContext.EvaluateFormulaAsync(param.参数值);
                if (value1 is ArrayList)
                {
                     value = JsonConvert.SerializeObject(param.参数值, Formatting.Indented);
                }
                else
                {
                    value = (await dataContext.EvaluateFormulaAsync(param.参数值))?.ToString() ?? string.Empty;
                }
                             
                // 将参数名和参数值转换为字符串并添加到字典中
                cardDataCardParamMap[key] = value;
            }
            //组装到对象返回
            var cardData = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCardData
            {
                CardParamMap = cardDataCardParamMap,
            };
            return cardData;
        }

        //定义用户的私有数据转换格式方法
        public static async Task<Dictionary<string, AlibabaCloud.SDK.Dingtalkcard_1_0.Models.PrivateDataValue>> ProcessUserPrivateDataAsync(IServerCommandExecuteContext dataContext, IEnumerable<siyoushuju> privateDataItems)
        {
            // 创建一个字典来存储最终的结果数据
            var privateData = new Dictionary<string, AlibabaCloud.SDK.Dingtalkcard_1_0.Models.PrivateDataValue>();
            if(privateDataItems != null)
            {
                // 遍历用户私有数据列表
                foreach (var privateDataItem in privateDataItems)
                {
                    var userId = (await dataContext.EvaluateFormulaAsync(privateDataItem.用户userId))?.ToString() ?? string.Empty;

                    // 检查用户 ID 是否为空
                    if (string.IsNullOrEmpty(userId))
                    {
                         CreateErrorJson(102, "用户私有数据：userid不能为空");
                        break; // 停止执行
                    }

                    // 创建一个字典来存储卡片数据内容
                    var cardParamMap = new Dictionary<string, string>();
                    if(privateDataItem.用户私有数据内容 != null)
                    {
                        // 遍历卡片模板内容替换参数列表
                        foreach (var param in privateDataItem.用户私有数据内容)
                        {
                            // 先检查 param.参数名 是否为空
                            if (string.IsNullOrEmpty(Convert.ToString(param.参数名)))
                            {
                                 CreateErrorJson(103, "用户私有数据：内容的参数名不能为空");
                                break; // 停止执行

                            }

                            // 将参数名和参数值转换为字符串并添加到字典中
                            var key = (await dataContext.EvaluateFormulaAsync(param.参数名))?.ToString() ?? string.Empty;
                            var value = (await dataContext.EvaluateFormulaAsync(param.参数值))?.ToString() ?? string.Empty;

                            if (!string.IsNullOrEmpty(key) && value != null)
                            {
                                cardParamMap[key] = value;
                            }
                        }
                    }
                    else
                    {
                        cardParamMap = null;
                    }
                    
                    // 添加数据到字典
                    if (cardParamMap.Count > 0)
                    {
                        privateData[userId] = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.PrivateDataValue
                        {
                            CardParamMap = cardParamMap
                        };
                    }
                }
            }
            else
            {
                privateData = null;
            }
            return privateData;
        }

        //定义动态数据源配置取值转换方法
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestOpenDynamicDataConfig> dongtaishujuyuan(IServerCommandExecuteContext dataContext, IEnumerable<dongtaishuju> privateDataItems)
        {
            // 初始化最终结果数据
            var aaaa = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestOpenDynamicDataConfig();

            // 创建一个列表来存储动态数据源配置
            var dynamicDataSourceConfigsList = new List<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestOpenDynamicDataConfig.CreateAndDeliverRequestOpenDynamicDataConfigDynamicDataSourceConfigs>();
            if (privateDataItems != null)
            {
                // 遍历数据列表
                foreach (var item in privateDataItems)
                {
                    var dynamicDataSourceId = ( dataContext.EvaluateFormulaAsync(item.数据源ID))?.ToString() ?? string.Empty;
                    // 检查用户 ID 是否为空
                    if (string.IsNullOrEmpty(dynamicDataSourceId))
                    {
                         CreateErrorJson(103, "动态数据源配置：数据源的唯一 ID不能为空");
                        break; // 停止执行
                    }
                    // 创建一个字典来存储卡片数据内容
                    var openDynamicDataConfigDynamicDataSourceConfigs0ConstParams = new Dictionary<string, string>();
                    if (item.回调固定参数 != null)
                    {
                        foreach (var item1 in item.回调固定参数)
                        {
                            // 先检查 param.参数名 是否为空
                            if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                            {
                                 CreateErrorJson(104, "动态数据源配置：回调数据源时回传的固定参数名不能为空");
                                break; // 停止执行
                            }
                            // 将参数名和参数值转换为字符串并添加到字典中
                            var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                            var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                            if (!string.IsNullOrEmpty(key) && value != null)
                            {
                                openDynamicDataConfigDynamicDataSourceConfigs0ConstParams[key] = value;
                            }

                        }
                    }
                    else
                    {
                        openDynamicDataConfigDynamicDataSourceConfigs0ConstParams = null;
                    }
                        
                    //取拉取策略
                    var PullStrategy = item.动态数据源配置.拉取策略;
                    var Interval = item.动态数据源配置.拉取的间隔时间;
                    var TimeUnit = item.动态数据源配置.拉取间隔的时间单位;
                    if (item.动态数据源配置.拉取策略 == "INTERVAL")
                    {
                        Interval = item.动态数据源配置.拉取的间隔时间;
                        TimeUnit = item.动态数据源配置.拉取间隔的时间单位;
                    }
                    else
                    {
                        Interval = 30;
                        TimeUnit = null;
                    }
                    // 创建拉取配置
                    var openDynamicDataConfigDynamicDataSourceConfigs0PullConfig = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestOpenDynamicDataConfig.CreateAndDeliverRequestOpenDynamicDataConfigDynamicDataSourceConfigs.CreateAndDeliverRequestOpenDynamicDataConfigDynamicDataSourceConfigsPullConfig
                    {
                        PullStrategy = PullStrategy,
                        Interval = Interval,
                        TimeUnit = TimeUnit,
                    };
                    // 创建动态数据源配置
                    var openDynamicDataConfigDynamicDataSourceConfigs0 = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestOpenDynamicDataConfig.CreateAndDeliverRequestOpenDynamicDataConfigDynamicDataSourceConfigs
                    {
                        DynamicDataSourceId = dynamicDataSourceId,
                        ConstParams = openDynamicDataConfigDynamicDataSourceConfigs0ConstParams,
                        PullConfig = openDynamicDataConfigDynamicDataSourceConfigs0PullConfig,
                    };
                    // 将配置添加到列表中
                    dynamicDataSourceConfigsList.Add(openDynamicDataConfigDynamicDataSourceConfigs0);
                }
                // 设置 DynamicDataSourceConfigs 属性
                aaaa.DynamicDataSourceConfigs = dynamicDataSourceConfigsList;
            }
            else { aaaa = null;}
            return aaaa;
        }

        //IM单聊酷应用场域信息
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenSpaceModel> imdanliaopeizhi(IServerCommandExecuteContext dataContext, SharedChatConfig config)
        {
            //设置默认变量
            bool aa01 = false; //是否支持转发
            string aa02 = null; //类型的icon
            string aa03 = null; //卡片类型名
            string aa04 = null; //供消息展示与搜索字段  卡片的具体描述
            string aa05 = null; //通知内容
            bool aa06 = false; //是否关闭推送通知
            var aa07 = new Dictionary<string, string>();//卡片摘要
            //取通知内容变量
            if (config != null)
            {

                aa01 = config.是否支持转发;
                aa02 = (await dataContext.EvaluateFormulaAsync(config.类型的icon))?.ToString() ?? string.Empty;
                aa03 = (await dataContext.EvaluateFormulaAsync(config.卡片类型名))?.ToString() ?? string.Empty;
                aa04 = (await dataContext.EvaluateFormulaAsync(config.供消息展示与搜索字段))?.ToString() ?? string.Empty;
                aa05 = (await dataContext.EvaluateFormulaAsync(config.通知内容))?.ToString() ?? string.Empty;
                aa06 = config.是否关闭推送通知;
                foreach (var item1 in config.卡片摘要)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.语言类别)))
                    {
                         CreateErrorJson(105, "IM群聊配置：卡片摘要的语言类别不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.语言类别))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        aa07[key] = value;
                    }
                }
                var imGroupOpenSpaceModelNotification = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenSpaceModel.CreateAndDeliverRequestImSingleOpenSpaceModelNotification
                {
                    AlertContent = aa05,
                    NotificationOff = aa06,
                };
                var imGroupOpenSpaceModelSearchSupport = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenSpaceModel.CreateAndDeliverRequestImSingleOpenSpaceModelSearchSupport
                {
                    SearchIcon = aa02,
                    SearchTypeName = aa03,
                    SearchDesc = aa04,
                };
                var imGroupOpenSpaceModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenSpaceModel
                {
                    SupportForward = aa01,
                    LastMessageI18n = aa07,
                    SearchSupport = imGroupOpenSpaceModelSearchSupport,
                    Notification = imGroupOpenSpaceModelNotification,
                };
                return imGroupOpenSpaceModel;

            }
            else
            {
                 CreateErrorJson(105, "IM群聊配置：数据内容不能为空");
                // 停止执行
                return null;
            }  
        }

        //IM群聊场域信息配置信息
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenSpaceModel> imqunliaopeizhi(IServerCommandExecuteContext dataContext, SharedChatConfig config)
        {
            //设置默认变量
            bool aa01 = false; //是否支持转发
            string aa02 = null; //类型的icon
            string aa03 = null; //卡片类型名
            string aa04 = null; //供消息展示与搜索字段  卡片的具体描述
            string aa05 = null; //通知内容
            bool aa06 = false; //是否关闭推送通知
            // 创建一个字典来存储卡片数据内容
            var aa07 = new Dictionary<string, string>();
            aa01 = config.是否支持转发;
            aa02 = (await dataContext.EvaluateFormulaAsync(config.类型的icon))?.ToString() ?? string.Empty;
            aa03 = (await dataContext.EvaluateFormulaAsync(config.卡片类型名))?.ToString() ?? string.Empty;
            aa04 = (await dataContext.EvaluateFormulaAsync(config.供消息展示与搜索字段))?.ToString() ?? string.Empty;
            aa05 = (await dataContext.EvaluateFormulaAsync(config.通知内容))?.ToString() ?? string.Empty;
            aa06 = config.是否关闭推送通知;
            if (config.卡片摘要 != null)
            {
                foreach (var item1 in config.卡片摘要)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.语言类别)))
                    {
                        CreateErrorJson(105, "IM群聊配置：卡片摘要的语言类别不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.语言类别))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        aa07[key] = value;
                    }
                }
            }
            else
            {
                aa07 = null;
            }

            var imGroupOpenSpaceModelNotification = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenSpaceModel.CreateAndDeliverRequestImGroupOpenSpaceModelNotification
            {
                AlertContent = aa05,
                NotificationOff = aa06,
            };
            var imGroupOpenSpaceModelSearchSupport = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenSpaceModel.CreateAndDeliverRequestImGroupOpenSpaceModelSearchSupport
            {
                SearchIcon = aa02,
                SearchTypeName = aa03,
                SearchDesc = aa04,
            };
            var imGroupOpenSpaceModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenSpaceModel
            {
                SupportForward = aa01,
                LastMessageI18n = aa07,
                SearchSupport = imGroupOpenSpaceModelSearchSupport,
                Notification = imGroupOpenSpaceModelNotification,
            };
            return imGroupOpenSpaceModel;   
        }
        //IM机器人单聊场域信息
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel> imjiqirenpeizhi(IServerCommandExecuteContext dataContext, SharedChatConfig config)
        {
            //设置默认变量
            bool aa01 = false; //是否支持转发
            string aa02 = null; //类型的icon
            string aa03 = null; //卡片类型名
            string aa04 = null; //供消息展示与搜索字段  卡片的具体描述
            string aa05 = null; //通知内容
            bool aa06 = false; //是否关闭推送通知
            var aa07 = new Dictionary<string, string>();
            //取通知内容变量
            if (config != null)
            {

                aa01 = config.是否支持转发;
                aa02 = (await dataContext.EvaluateFormulaAsync(config.类型的icon))?.ToString() ?? string.Empty;
                aa03 = (await dataContext.EvaluateFormulaAsync(config.卡片类型名))?.ToString() ?? string.Empty;
                aa04 = (await dataContext.EvaluateFormulaAsync(config.供消息展示与搜索字段))?.ToString() ?? string.Empty;
                aa05 = (await dataContext.EvaluateFormulaAsync(config.通知内容))?.ToString() ?? string.Empty;
                aa06 = config.是否关闭推送通知;
                if (config.卡片摘要 != null)
                {
                    foreach (var item1 in config.卡片摘要)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.语言类别)))
                        {
                             dingcard.CreateErrorJson(105, "IM群聊配置：卡片摘要的语言类别不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var key = (await dataContext.EvaluateFormulaAsync(item1.语言类别))?.ToString() ?? string.Empty;
                        var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                        if (!string.IsNullOrEmpty(key) && value != null)
                        {
                            aa07[key] = value;
                        }
                    }
                }
                else
                {
                    aa07 = null;
                }
                var imGroupOpenSpaceModelNotification = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel.CreateAndDeliverRequestImRobotOpenSpaceModelNotification
                {
                    AlertContent = aa05,
                    NotificationOff = aa06,
                };
                var imGroupOpenSpaceModelSearchSupport = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel.CreateAndDeliverRequestImRobotOpenSpaceModelSearchSupport
                {
                    SearchIcon = aa02,
                    SearchTypeName = aa03,
                    SearchDesc = aa04,
                };
                var imGroupOpenSpaceModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel
                {
                    SupportForward = aa01,
                    LastMessageI18n = aa07,
                    SearchSupport = imGroupOpenSpaceModelSearchSupport,
                    Notification = imGroupOpenSpaceModelNotification,
                };
                return imGroupOpenSpaceModel;
            }
            else
            {
                 dingcard.CreateErrorJson(105, "IM群聊配置：数据内容不能为空");
                // 停止执行
                return null;
            }         
        }

        //协作场域信息
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenSpaceModel> getxiezuopeizhi(IServerCommandExecuteContext dataContext,xiezuo aaaa)
        {
            string Title = null;
            string CoolAppCode = null;
            if (aaaa != null)
            {
                if (aaaa.卡片标题 != null)
                {
                    // 将参数名和参数值转换为字符串并添加到字典中
                    Title = (await dataContext.EvaluateFormulaAsync(aaaa.卡片标题))?.ToString() ?? string.Empty;
                    CoolAppCode = (await dataContext.EvaluateFormulaAsync(aaaa.酷应用编码))?.ToString() ?? string.Empty;
                }
                else
                {
                     CreateErrorJson(106, "协作场域信息：卡片标题不能为空");
                }
                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenSpaceModel coFeedOpenSpaceModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenSpaceModel
                {
                    Title = Title,
                    CoolAppCode = CoolAppCode,
                };
                return coFeedOpenSpaceModel;
            }
            else
            {
                 dingcard.CreateErrorJson(106, "协作场域信息：数据内容不能为空");
                // 停止执行
                return null;
            }         
        }
        //吊顶场域属性
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenSpaceModel> getdiaoding(IServerCommandExecuteContext dataContext, object aaaa)
        {
            string SpaceType = null;
            if (aaaa != null)
            {
                SpaceType = (await dataContext.EvaluateFormulaAsync(aaaa))?.ToString() ?? string.Empty;
                var topOpenSpaceModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenSpaceModel
                {
                    SpaceType = SpaceType,
                };
                return topOpenSpaceModel;

            }
            else
            {
                 dingcard.CreateErrorJson(107, "吊顶场域信息：数据内容不能为空");
                return null;
            }
           
        }
        //单聊酷应用场域投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenDeliverModel> danliaokuyingyong(IServerCommandExecuteContext dataContext, danliaokutoufang aaaa)
        {
            var bbb = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenDeliverModel();
            if (aaaa != null)
            {              
                var UserIds = new Dictionary<string, string>();
                foreach (var item1 in aaaa.AT用户)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                    {
                         CreateErrorJson(108, "单聊酷应用投放参数：AT用户内容不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;
                    var user = (await dataContext.EvaluateFormulaAsync(item1.用户名))?.ToString() ?? string.Empty;

                    if (userId != null && user != null)
                    {
                        UserIds[userId] = user;
                    }
                }
                var kuozhan = new Dictionary<string, string>();

                foreach (var item1 in aaaa.扩展字段)
                    {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                    {
                         CreateErrorJson(108, "单聊酷应用投放参数：扩展字段参数不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                            kuozhan[key] = value;
                    }
                }
                //组装对象数据并设置对象的对应值              
                var bbbType = bbb.GetType();
                var atUserIdsProperty = bbbType.GetProperty("atUserIds");
                var extensionProperty = bbbType.GetProperty("extension");

                if (atUserIdsProperty != null)
                {
                    atUserIdsProperty.SetValue(bbb, UserIds);
                }

                if (extensionProperty != null)
                {
                    extensionProperty.SetValue(bbb, kuozhan);
                }
                return bbb;
            }
            else { CreateErrorJson(108, "单聊酷应用投放参数：数据内容不能为空");
                return null;
            }         
        }
        //群聊投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel> qunliaotoufang(IServerCommandExecuteContext dataContext, qunliao aaaa)
        {
            string robotCode = null;
            var UserIds = new Dictionary<string, string>();
            var Recipients = new List<string>();
            var extension = new Dictionary<string, string>();
            if (aaaa != null)
            {
                if (aaaa.机器人robotCode != null)
                {
                    robotCode = (await dataContext.EvaluateFormulaAsync(aaaa.机器人robotCode))?.ToString() ?? string.Empty; 
                }
                if (aaaa.AT用户 != null)
                {
                    foreach (var item1 in aaaa.AT用户)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                        {
                             CreateErrorJson (109, "群聊投放参数：AT用户内容不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;
                        var user = (await dataContext.EvaluateFormulaAsync(item1.用户名))?.ToString() ?? string.Empty;

                        if (userId != null && user != null)
                        {
                            UserIds[userId] = user;
                        }
                    }
                }
                if (aaaa.接收人userId列表 != null)
                {
                    foreach (var item1 in aaaa.接收人userId列表)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                        {
                             CreateErrorJson(109, "群聊投放参数：接收人列表不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;

                        if (userId != null)
                        {
                            Recipients.Add(userId);
                        }
                    }
                }
 
                if (aaaa.扩展字段 != null)
                {
                    foreach (var item1 in aaaa.扩展字段)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                        {
                             CreateErrorJson(109, "群聊投放参数：扩展字段参数不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                        var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                        if (!string.IsNullOrEmpty(key) && value != null)
                        {
                            extension[key] = value;
                        }
                    }
                }              
                var imGroupOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel
                {
                    RobotCode = robotCode,
                    AtUserIds = UserIds,
                    Recipients = Recipients,
                    Extension = extension,
                };
                return imGroupOpenDeliverModel;

            }
            else
            {
                 CreateErrorJson(109, "群聊投放参数：数据内容不能为空");
                return null;
            }      
        }
        //IM机器人单聊投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel> imjiqirendanliaotoufang(IServerCommandExecuteContext dataContext, imjqrtoufang aaaa)
        {
            string spaceType = null;
            string robotCode = null;
            var extension = new Dictionary<string, string>();
            if (aaaa != null )
            {
                if(aaaa.spaceType != null)
                {
                    spaceType = (await dataContext.EvaluateFormulaAsync(aaaa.spaceType))?.ToString() ?? string.Empty; 
                }
                else
                {
                     CreateErrorJson(110, "IM机器人单聊投放参数：spaeType参数不能为空");
                }
                if(aaaa.机器人robotCode != null)
                {
                    robotCode = ( dataContext.EvaluateFormulaAsync(aaaa.机器人robotCode))?.ToString() ?? string.Empty;
                }
                else
                {
                     CreateErrorJson(110, "IM机器人单聊投放参数：机器人robotCode不能为空");
                }
                foreach (var item1 in aaaa.扩展字段)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                    {
                         CreateErrorJson(109, "群聊投放参数：扩展字段参数不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        extension[key] = value;
                    }
                }
                var imRobotOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel
                {
                    SpaceType = spaceType,
                    RobotCode = robotCode,
                    Extension = extension,
                };
                return imRobotOpenDeliverModel;
            }
            else
            {
                 CreateErrorJson(110, "IM机器人单聊投放参数：数据内容不能为空");
                return null;
            }    
        }
        //吊顶投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenDeliverModel> diaodingtoufang(IServerCommandExecuteContext dataContext, diaodingtoufang aaaa)
        {
            long expiredTimeMillis = 0;
            var UserIds = new List<string>();
            var Platforms = new List<string>();
            if (aaaa != null)
            {
                if (aaaa.过期时间戳 != null)
                {
                    string expiredTimeMillisString = (await dataContext.EvaluateFormulaAsync(aaaa.过期时间戳))?.ToString() ?? string.Empty;
                    if (long.TryParse(expiredTimeMillisString, out long cccc))
                    {
                        // 转换成功，使用 expiredTimeMillis
                        expiredTimeMillis = cccc;
                    }
                    else
                    {
                         dingcard.CreateErrorJson(111, "吊顶投放参数：无效的过期时间戳格式");
                    } 
                }
                foreach (var item1 in aaaa.可以查看卡片的用户列表)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                    {
                         dingcard.CreateErrorJson(111, "吊顶投放参数：可以查看卡片的用户列表不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;

                    if (userId != null)
                    {
                        UserIds.Add(userId);
                    }
                }
                foreach (var item1 in aaaa.可以查看吊顶卡片的设备)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.选择设备)))
                    {
                        dingcard.CreateErrorJson(111, "吊顶投放参数：可以查看卡片的用户列表不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var ddd = item1.选择设备;

                    if (ddd != null)
                    {
                        Platforms.Add(ddd);
                    }
                }
                var topOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenDeliverModel
                {
                    ExpiredTimeMillis = expiredTimeMillis,
                    UserIds = UserIds,
                    Platforms = Platforms,
                };
                return topOpenDeliverModel;
            }
            else
            {
                 dingcard.CreateErrorJson(111, "吊顶投放参数：数据内容不能为空");
                return null;
            }  
        }
        //协作投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenDeliverModel> xiezuotoufangcanshu (IServerCommandExecuteContext dataContext, xiezuotoufang aaaa)
        {
            string bbb = null;
            long ccc = 0;
            if (aaaa != null)
            {
                if (aaaa.业务标识 != null)
                {
                    bbb = (await dataContext.EvaluateFormulaAsync(aaaa.业务标识))?.ToString() ?? string.Empty; 
                }
                else
                {
                     dingcard.CreateErrorJson(112, "协作投放参数：业务标识不能为空");
                }
                if(aaaa.排序时间 != null)
                {
                    string expiredTimeMillisString = ( dataContext.EvaluateFormulaAsync(aaaa.排序时间))?.ToString() ?? string.Empty;
                    if (long.TryParse(expiredTimeMillisString, out long cccc))
                    {
                        // 转换成功，使用 expiredTimeMillis
                        ccc = cccc;
                    }
                    else
                    {
                         dingcard.CreateErrorJson(112, "协作投放参数：排序时间不能为空或时间戳格式错误");
                    }
                }
                var coFeedOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenDeliverModel
                {
                    BizTag = bbb,
                    GmtTimeLine = ccc,
                };
                return coFeedOpenDeliverModel;

            }
            else
            {
                 dingcard.CreateErrorJson(112, "协作投放参数：数据内容不能为空");
                return null;
            }           
        }
        //文档投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestDocOpenDeliverModel> wendangtoufangcanshu(IServerCommandExecuteContext dataContext, userid aaaa)
        {
            string userid = null;
            if(aaaa != null)
            {
                if (aaaa.userId != null)
                {
                    userid = (await dataContext.EvaluateFormulaAsync(aaaa.userId))?.ToString() ?? string.Empty;
                }
                else
                {
                     dingcard.CreateErrorJson(113, "文档投放参数：userid不能为空");
                }
            }
            else
            {
                 dingcard.CreateErrorJson(113, "文档投放参数：数据内容不能为空");
            }
            var docOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestDocOpenDeliverModel
            {
                UserId = userid,
            };
            return docOpenDeliverModel;
        }
        //场景ID数据转换
        public static async Task<string> getchangjingid (IServerCommandExecuteContext dataContext ,string bbb, List<changjingid> aaaa)
        {
            //设置一个列表
            var fff = new List<string>();
            foreach (var item1 in aaaa)
            {
                //公式计算
                string ccc = (await dataContext.EvaluateFormulaAsync(item1.SpaceId))?.ToString() ?? string.Empty;
                // 将参数名和参数值转换为字符串并添加到字典中
                var ddd = "dtv1.card//" + bbb + "." + ccc;

                if (ddd != null)
                {
                    fff.Add(ddd);
                }
            }
            // 列表转换为字符串
            string result = string.Join(",", fff);

            return result;
        }
        //回调注册方法
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.RegisterCallbackRequest> huidiaozhuce (IServerCommandExecuteContext dataContext,  huidiaoshezhi aaaa)
        {
            var aaa = (await dataContext.EvaluateFormulaAsync(aaaa.回调路由Key))?.ToString() ?? string.Empty;
            var bbb = (await dataContext.EvaluateFormulaAsync(aaaa.回调URL地址))?.ToString() ?? string.Empty;
            var ccc = (await dataContext.EvaluateFormulaAsync(aaaa.加密密钥))?.ToString() ?? string.Empty;

            var registerCallbackRequest = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.RegisterCallbackRequest
            {
                CallbackRouteKey = aaa,
                CallbackUrl = bbb,
                ApiSecret = ccc,
                ForceUpdate = aaaa.是否强制覆盖回调地址,
            };
            return registerCallbackRequest;
        }
         
    }
}
